/**
 *
 * Created by Pan,hongxue, 8/30/2011 5:19:40 PM
 *
 */

function RandomProxy(randomSeed)
{
	this._randomSeed = randomSeed;

	this.cg_hun = function () {
		var _loc_4 = 0;
		this._cgStr = "";
		var _loc_1 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890";
		var _loc_2 = _loc_1.length;
		var _loc_3 = 0;
		while (_loc_3 < _loc_2)
		{
			_loc_4 = (this.ran() * _loc_1.length) | 0;
			this._cgStr = this._cgStr + _loc_1.charAt(_loc_4);
			_loc_1 = _loc_1.split(_loc_1.charAt(_loc_4)).join("");
			_loc_3++;
		}
	};

	this.ran = function () {
		this._randomSeed = (this._randomSeed * 211 + 30031) % 65536;
		return this._randomSeed / 65536;
	};

	this.cg_fun = function (param1) {
		var _loc_2 = param1.split("*");
		var _loc_3 = "";
		var _loc_4 = 0;

		while (_loc_4 < (_loc_2.length - 1))
		{
			_loc_3 = _loc_3 + this._cgStr.charAt(Number(_loc_2[_loc_4]));
			_loc_4++;
		}
		return _loc_3;
	};

	this.cg_hun();
}

function getFileId(param1/*:Object*/, param2/*:String*/, param3/*:int*/, param4/*:RandomProxy*/)/* : String*/
{
	var _loc_6/*:String8*/ = null;
	var _loc_5/*:String*/ = "";
	for (_loc_6 in param1)
	{
		if (_loc_6 == param2)
		{
			_loc_5 = param1[_loc_6];
			break;
		}
	}

	if (_loc_5 == "")
	{
		return "";
	}

	var _loc_7/*:String*/ = "";
	var _loc_8 = param4.cg_fun(_loc_5);
	var _loc_9 = param4.cg_fun(_loc_5).slice(0, 8);
	var _loc_10 = (param3) | 0;
	var _loc_11 = (param3 | 0).toString(16);

	if ((param3 | 0).toString(16).length == 1)
	{
		_loc_11 = "0" + _loc_11;
	}

	_loc_11 = _loc_11.toUpperCase();
	var _loc_12 = _loc_8.slice(10, _loc_8.length);
	_loc_7 = _loc_9 + _loc_11 + _loc_12;

	return _loc_7;
}// end function


function validateVideoUrl(ori_url) 
{
	var html = download_page(ori_url);
	var matches = html.match(/videoId2= '(.*?)'/);
	if (matches == null) {
		// vid not found
		return 'NotFoundVideo';
	}
	var dst_url = 'http://v.youku.com/player/getPlayList/VideoIDS/' + matches[1];
	var json_str = download_page(dst_url);
	var json = eval('(' + json_str + ')');

	var data = json.data[0];
	var random = new RandomProxy(data.seed);
	var sb = [];

	for (var ext in data.segs) {
		var segs = data.segs[ext];
		for (var i in segs) {
                  sb.push('http://f.youku.com/player/getFlvPath/st/' + (ext == 'hd2' ? 'flv' : ext) + '/fileid/' + getFileId(data.streamfileids, ext, i, random) + '?K=' + segs[i].k + '&hd=' + (ext == 'hd2' ? 2 : 0));
		}
	}
    if(sb.length){
       //matches = html.match(/<title>(.*?)<\/title>/);
       matches = html.match(/<title>([\s\S]*?)<\/title>/);
       if(matches){
    	   return 'success_title:' + encodeURIComponent(matches[1])
    	          + '\n video_urls:' + encodeURIComponent(sb)
    	   		  + '\n seconds:' + Math.round(parseInt(data.seconds));              
       }
    }
	return 'error:empty_path';
}

function js_get_media_url(ori_url) 
{
	var html = download_page(ori_url);
	var matches = html.match(/videoId2= '(.*?)'/);
	if (matches == null) {
		// vid not found
		return '';
	}
	var dst_url = 'http://v.youku.com/player/getPlayList/VideoIDS/' + matches[1];
	var json_str = download_page(dst_url);
	var json = eval('(' + json_str + ')');

	var data = json.data[0];
	var random = new RandomProxy(data.seed);
	var sb = [];

	
	for (var ext in data.segs) {
		if(data.segs['flv']){
			ext = 'flv';
		}
		var segs = data.segs[ext];
		for (var i in segs) {
			sb.push('http://f.youku.com/player/getFlvPath/st/' + (ext == 'hd2' ? 'flv' : ext) + '/fileid/' + getFileId(data.streamfileids, ext, i, random) + '?K=' + segs[i].k + '&hd=' + (ext == 'hd2' ? 2 : 0));
		}	
		break;
	}
	return sb;
}
